package com.leyou.item.web;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@RestController
@RequestMapping("spec")
public class SpecController {
    @Autowired
    private SpecGroupService specGroupService;
    @Autowired
    private SpecParamService specParamService;

    @GetMapping("groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> listGroupByCategoryId(@RequestParam("id")Long cid){
        return ResponseEntity.ok(
                SpecGroupDTO.convertEntityList(
                        specGroupService.query().eq("category_id",cid).list()
                )
        );
    }

    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> listParams(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId",required = false)Long groupId,
            @RequestParam(value = "searching",required = false)Boolean searching
    ){
        return ResponseEntity.ok(SpecParamDTO.convertEntityList(specParamService.queryParams(categoryId,groupId,searching)));
    }

    /**
     * 查询规格组及规格参数
     * @param categoryId
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id")Long categoryId){
        return ResponseEntity.ok(specGroupService.querySpecList(categoryId));
    }

    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.save(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.updateById(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id删除规格组
     * @param id
     * @return
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable("id")Long id){
        specGroupService.removeById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.save(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.updateById(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id删除规格参数
     * @param id
     * @return
     */
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteParam(@PathVariable("id")Long id){
        specParamService.removeById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
